﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TamoCRM.Core.Common.Utilities;
using TamoCRM.Core.ComponentModel;
using TamoCRM.Core.Instrumentation;


namespace TamoCRM.Core.Data
{
    public abstract class DataProvider
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(DataProvider));
        #region Shared/Static Methods

        public static DataProvider Instance()
        {
            return ComponentFactory.GetComponent<DataProvider>();
        }

        #endregion

        #region Public Properties

        public virtual string ConnectionString
        {
            get
            {
                //Get Connection string from web.config
                var connectionString = Config.GetConnectionString();
                if (string.IsNullOrEmpty(connectionString))
                {
                    //Use connection string specified in provider
                    connectionString = Settings["connectionString"];
                }
                return connectionString;
            }
        }

        public virtual string DatabaseOwner
        {
            get
            {
                var databaseOwner = Settings["databaseOwner"];
                if (!string.IsNullOrEmpty(databaseOwner) && databaseOwner.EndsWith(".") == false)
                {
                    databaseOwner += ".";
                }
                return databaseOwner;
            }
        }

        public string DefaultProviderName
        {
            get
            {
                return Instance().ProviderName;
            }
        }

        public abstract bool IsConnectionValid { get; }

        public virtual string ObjectQualifier
        {
            get
            {
                var objectQualifier = Settings["objectQualifier"];
                if (!string.IsNullOrEmpty(objectQualifier) && objectQualifier.EndsWith("_") == false)
                {
                    objectQualifier += "_";
                }
                return objectQualifier;
            }
        }

        public virtual string ProviderName
        {
            get
            {
                return Settings["providerName"];
            }
        }

        public virtual string ProviderPath
        {
            get
            {
                return Settings["providerPath"];
            }
        }

        public abstract Dictionary<string, string> Settings { get; }

        #endregion

        #region Private Methods

        private DateTime FixDate(DateTime dateToFix)
        {
            //Fix for Sql Dates having a minimum value of 1/1/1753
            if (dateToFix < Constances.SqlDateTime.MinValue)
            {
                dateToFix = Constances.SqlDateTime.MinValue;
            }
            return dateToFix;
        }

        private object GetRoleNull(int RoleID)
        {
            if (RoleID.ToString(CultureInfo.InvariantCulture) == string.Empty)
            {
                return DBNull.Value;
            }
            return RoleID;
        }

        #endregion
    }
}
